﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;
using System.Data.Objects;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class PlatformDao : DataAccessObject
    {
        #region Add

        public void AddPlatform(Platform platform)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddToplatform(platform);
                    
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyPlatform(Platform platform)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    Platform oldPlatform = entities.platform.FirstOrDefault(e => e.platformId == platform.platformId);
                    oldPlatform = platform;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Get

        public Platform GetPlatformByName(string platformNombre)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.platform.FirstOrDefault(e =>
                           e.name.CompareTo(platformNombre) ==0);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public List<Platform> GetPlatforms()
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.platform
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
